#!/usr/bin/env python
try:
    import json
except:
    from helper import HelpJson as json
import sys
import types

__metaclass__ = type #assure to use new class in python 3.0

class UserNode(object):
    """
    UserNode
    Mapping from userrelation.log
    Note that the key user ID is not included.
    So you should use a dict if needed, such as <key_user_id, instance_of_UserNode>
    """
    _id = None
    _time = None
    _type = None

    def __init__(self, id='', time=0, type=''):
        self._id = id
        self._time = time
        self._type = type

    def init(self, line):
        line = line.strip()
        line_vec = line.split(':')
        self._id = line_vec[0]
        self._time = int(line_vec[1])
        self._type = line_vec[2]

    def __str__(self):
        return '%s:%d:%s' % (self._id, self._time, self._type)

    def isSameId(self, string):
        return self._id == string

    def getId(self):
        return self._id

    def getTime(self):
        return self._time

    def getType(self):
        return self._type

    def setTime(self, time):
        self._time = time

class RecomTag(object):
    _type = None
    _time = None
    _related_id = None

    def __init__(self, tp):
        self._type = tp
        self._time = ''
        self._related_id = ''

    def isType(self, tp):
        return self._type == tp

    def getType(self):
        return self._type

    def getRelatedId(self):
        return self._related_id

    def setTime(self, tm):
        self._time = int(tm)

    def getTime(self):
        return self._time

    def setRelatedId(self, rid):
        self._related_id = rid

    def __str__(self):
        return '%s:%s:%s' % (self._type, self._time, self._related_id)

    def init(self, line):
        line = line.strip()
        try:
            self._type = line.split(':')[0]
            self._time = line.split(':')[1]
            self._related_id = line.split(':')[2]
        except:
            pass

class RecomTagFeed(RecomTag):
    _uid = None
    _is_result = None

    def __init__(self, tp):
        super(RecomTagFeed, self).__init__(tp)
        self._uid = ''
        self._is_result = False

    @staticmethod
    def isMumber(line):
        try:
            if len(line.split(':')) == 5:
                return True
            else:
                return False
        except:
            return False

    def getUid(self):
        return self._uid

    def setUid(self, rid):
        self._uid = rid

    def getIsResult(self):
        if type(self._is_result) is types.StringType:
            if self._is_result == 'False':
                return False
            else:
                return True
        return self._is_result

    def setIsResult(self, flag):
        self._is_result = flag

    def __str__(self):
        return '%s:%s:%s:%s:%s' % (self._type, self._time, self._related_id, self._uid, self._is_result)

    def init(self, line):
        line = line.strip()
        try:
            [self._type, self._time, self._related_id, self._uid, self._is_result] = line.split(':')
        except:
            pass


class RecomAtom(object):
    involved_ids = None
    involved_num = None
    item_id = None
    def __init__(self):
        self.involved_ids = []
        self.involved_num = 0
        self.item_id = ''
    def __str__(self):
        return str(self.__dict__)
    def __repr__(self):
        return str(self.__dict__)

class RecomResult(object):
    ppuid = None
    reason_id = None
    reason_desc = None
    expired_time = None
    resys_items = None
    def __init__(self):
        self.ppuid = ''
        self.reason_id = ''
        self.reason_desc = ''
        self.expired_time = 0
        self.resys_items = []

    def __str__(self):
        return str(self.__dict__)


